package com.ruoyi.web.controller.TiebaForums.controller;

import java.util.List;
import java.util.Arrays;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.web.controller.TiebaPosts.domain.TiebaPosts;
import com.ruoyi.web.controller.TiebaPosts.mapper.TiebaPostsMapper;
import com.ruoyi.web.controller.TiebaPosts.service.ITiebaPostsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.TiebaForums.domain.TiebaForums;
import com.ruoyi.web.controller.TiebaForums.service.ITiebaForumsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 贴吧管理Controller
 *
 * @author yong
 * @date 2025-03-29
 */
//@Api(tags="贴吧管理管理")
@RestController
@RequestMapping("/system/TiebaForums")
public class TiebaForumsAdminController extends BaseController
{
    @Autowired
    private ITiebaForumsService tiebaForumsService;

    @Resource
    private TiebaPostsMapper postsMapper;

    /**
     * 查询贴吧管理列表
     */
    @ApiOperation("查询贴吧管理列表")
    @PreAuthorize("@ss.hasPermi('system:TiebaForums:list')")
    @GetMapping("/list")
    public TableDataInfo list(TiebaForums tiebaForums)
    {
        startPage();
        List<TiebaForums> list = tiebaForumsService.selectTiebaForumsList(tiebaForums);
        return getDataTable(list);
    }

    /**
     * 导出贴吧管理列表
     */
    @ApiOperation("导出贴吧管理列表")
    @PreAuthorize("@ss.hasPermi('system:TiebaForums:export')")
    @Log(title = "贴吧管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TiebaForums tiebaForums)
    {
        List<TiebaForums> list = tiebaForumsService.selectTiebaForumsList(tiebaForums);
        ExcelUtil<TiebaForums> util = new ExcelUtil<TiebaForums>(TiebaForums.class);
        util.exportExcel(response, list, "贴吧管理数据");
    }

    /**
     * 获取贴吧管理详细信息
     */
    @ApiOperation("获取贴吧管理详细信息")
    @PreAuthorize("@ss.hasPermi('system:TiebaForums:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tiebaForumsService.getById(id));
    }

    /**
     * 新增贴吧管理
     */
    @ApiOperation("新增贴吧管理")
    @PreAuthorize("@ss.hasPermi('system:TiebaForums:add')")
    @Log(title = "贴吧管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TiebaForums tiebaForums)
    {
        return toAjax(tiebaForumsService.save(tiebaForums));
    }

    /**
     * 修改贴吧管理
     */
    @ApiOperation("修改贴吧管理")
    @PreAuthorize("@ss.hasPermi('system:TiebaForums:edit')")
    @Log(title = "贴吧管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TiebaForums tiebaForums)
    {
        return toAjax(tiebaForumsService.updateById(tiebaForums));
    }

    /**
     * 删除贴吧管理
     */
    @ApiOperation("删除贴吧管理")
    @PreAuthorize("@ss.hasPermi('system:TiebaForums:remove')")
    @Log(title = "贴吧管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        // 先删除对应的帖子
        for (Long tiebaId : ids) {
            // 查询该贴吧下所有的帖子并删除
            postsMapper.delete(new LambdaQueryWrapper<TiebaPosts>()
                    .eq(TiebaPosts::getForumId, tiebaId));
        }
        return toAjax(tiebaForumsService.removeByIds(Arrays.asList(ids)));
    }
}
